package com.poker.service;

import com.poker.consts.Code;
import com.poker.entity.ManagerInfo;
import com.poker.entity.User;
import com.poker.entity.UserLog;
import com.poker.repository.UserLogRepository;
import com.poker.repository.UserRepository;
import com.poker.util.Misc;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserService {

    @Resource
    private UserRepository repository;

    @Resource
    private UserLogRepository userLogRepository;

    public void save(User user) {
        repository.save(user);
    }

    public User findById(String id) { return repository.findById(id).get(); }

    @Transactional
    public void updateUserStateToNormal(User user, ManagerInfo managerInfo) {
        UserLog log = new UserLog();
        log.setUserId(user.getId());
        log.setOperateDate(new Date());
        log.setUserName(user.getUserName());
        log.setOperateId(managerInfo.getId());
        log.setOperateName(managerInfo.getUserName());
        log.setOperateAspect(Code.OperateAspect.MANAGER);
        log.setLogDetail("管理员执行用户解除账户锁定操作！");
        userLogRepository.save(log);

        repository.updateUserStateToNormal(Code.UserState.NORMAL, user.getId());
    }

    /**
     * @param: [page, size];
     * @return: org.springframework.data.domain.Page<User>;
     * @Description: 不带条件分页查询;
     */
    public Page<User> findUserNoCriteria(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");

        return addProperty(repository.findAll(pageable));
    }

    /**
     * @param: [page, size, workOrder];
     * @return: org.springframework.data.domain.Page<WorkOrder>;
     * @Description: 带条件的分页查询;
     */
    public Page<User> findUserCriteria(Integer page, Integer size, User user) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");

        return addProperty(repository.findAll((Specification<User>) (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();

            if (Misc.isNotEmpty(user.getUserName()))
                list.add(criteriaBuilder.equal(root.get("userName").as(String.class), user.getUserName()));

            Predicate[] predicateArray = new Predicate[list.size()];
            return criteriaBuilder.and(list.toArray(predicateArray));
        }, pageable));
    }

    private Page<User> addProperty(Page<User> pages) {
        for (User user: pages) { addProperty(user); }

        return pages;
    }

    private User addProperty(User user) {
        user.setUserStateName(Code.UserState.get(user.getUserState()));
        return user;
    }
}
